//倒计时
//设置结束时间
var end = new Date('2019/09/18 18:00:00');
//设置当前时分秒
var h = m = s = 0;

//定义一个秒杀函数
function a() {
	//获取当前的时间
	var now = new Date();
	//获取相差的时间
	var differ = end.getTime() - now.getTime();
	differ = parseInt(differ / 1000);

	if(differ >= 0) {
		h = parseInt(differ / 3600);
		m = parseInt((differ / 60) % 60);
		s = parseInt(differ % 60);

		if(h < 10) {
			h = '0' + h;
		}
		if(m < 10) {
			m = '0' + m;
		}
		if(s < 10) {
			s = '0' + s;
		}
	} else {
		clearInterval(t);
	}
	document.querySelector('#h').innerHTML = h;
	document.querySelector('#m').innerHTML = m;
	document.querySelector('#s').innerHTML = s;
}
a();

//添加计时器
var t = setInterval(function() {
	a();
}, 1000)

//换一换

//这是初始值
var page = 1;

function loadXMLDoc() {
	var xmlhttp;
	if(window.XMLHttpRequest) {
		//  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
		xmlhttp = new XMLHttpRequest();
	} else {
		// IE6, IE5 浏览器执行代码
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//转换为数组对象
			var json = JSON.parse(xmlhttp.responseText);
			//对数组进行循环
			for(let k = 0; k < json.shop.length; k++) {
				console.log(json.shop[k]);
				//创建HTML结构
				var img = document.createElement('img');
				img.src = json.shop[k].pic;
				var h3 = document.createElement('h3');
				h3.innerHTML = json.shop[k].title;
				var p = document.createElement('p');
				p.innerHTML = json.shop[k].desc;
				var p2 = document.createElement('p');
				p2.innerHTML = "￥";
				var span = document.createElement('span');
				span.innerHTML = json.shop[k].price;
				p2.appendChild(span);
				var li = document.createElement('li');
				li.appendChild(img);
				li.appendChild(h3);
				li.appendChild(p);
				li.appendChild(p2);
				console.log(li);
				document.querySelector('#list').appendChild(li);
			}
		}
	}
	xmlhttp.open("GET", "json/" + page + ".json", true);
	xmlhttp.send();
}

loadXMLDoc();

document.querySelector("#btn").onclick = function(){
	if(page >= 4){
		page = 1;
	}else{
		page++;
	}
	
	//先清空
	document.querySelector('#list').innerHTML = '';
	//再取数据放到list里面
	loadXMLDoc();
}
